<?php

/**
 * cl actions.
 *
 * @package    alainblondy
 * @subpackage cl
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class clActions extends sfActions
{

    public function executeIndex(sfWebRequest $request)
    {

    }

    public function executePresentation(sfWebRequest $request)
    {

    }

    public function executeVolumes(sfWebRequest $request)
    {
        $this->volumes = VolumeTable::getInstance()->findAll();
    }

    public function executeVolume(sfWebRequest $request)
    {
        $this->volume = $this->getRoute()->getObject();
        $page = $request->hasParameter('page') ? $request->getParameter('page') : 1;
        $this->pager = new sfDoctrinePager('Lettre');
        $this->pager->setMaxPerPage(10);
        $this->pager->setPage($page);
        $this->pager->setQuery(LettreTable::getInstance()->findAllByVolumeQuery($this->volume));
        $this->pager->init();
    }



    public function executeNavires(sfWebRequest $request)
    {
        if ($request->getParameter('page')) {
            $this->setNaviresPage($request->getParameter('page'));
        }

        $this->naviresPager = $this->getNaviresPager();
    }

    public function executeNaviresFilter(sfWebRequest $request)
    {
        $this->setNaviresPage(1);
        $this->naviresForm = new NaviresIndexFormFilter($this->getNaviresFilters());

        $this->naviresForm->bind($request->getParameter($this->naviresForm->getName()));
        if ($this->naviresForm->isValid()) {
            $this->setNaviresFilters($this->naviresForm->getValues());
            $this->redirect('navires');
        }

        $this->naviresPager = $this->getNaviresPager();

        $this->setTemplate('navires');
    }

    protected function getNaviresPager()
    {
        $pager = new sfDoctrinePager('Navire');
        $pager->setMaxPerPage(10);
        $pager->setPage($this->getNaviresPage());
        $pager->setQuery($this->buildNaviresQuery());
        $pager->init();

        return $pager;
    }

    protected function setNaviresPage($page)
    {
        $this->getUser()->setAttribute('navires.page', $page, 'frontend_module');
    }

    protected function getNaviresPage()
    {
        return $this->getUser()->getAttribute('navires.page', 1, 'frontend_module');
    }

    protected function buildNaviresQuery()
    {
        if (null === $this->naviresForm) {
            $this->naviresForm = new NaviresIndexFormFilter($this->getNaviresFilters());
        }
        $query = $this->naviresForm->buildQuery($this->getNaviresFilters());
        return $query;
    }

    protected function getNaviresFilters()
    {
        return $this->getUser()->getAttribute('navires.filters', array(), 'frontend_module');
    }

    protected function setNaviresFilters(array $filters)
    {
        return $this->getUser()->setAttribute('navires.filters', $filters, 'frontend_module');
    }




    public function executePersonnes(sfWebRequest $request)
    {
        if ($request->getParameter('page')) {
            $this->setPersonnesPage($request->getParameter('page'));
        }

        $this->personnesPager = $this->getPersonnesPager();
    }

    public function executePersonnesFilter(sfWebRequest $request)
    {
        $this->setPersonnesPage(1);
        $this->personnesForm = new PersonnesIndexFormFilter($this->getPersonnesFilters());

        $this->personnesForm->bind($request->getParameter($this->personnesForm->getName()));
        if ($this->personnesForm->isValid()) {
            $this->setPersonnesFilters($this->personnesForm->getValues());
            $this->redirect('personnes');
        }

        $this->personnesPager = $this->getPersonnesPager();

        $this->setTemplate('personnes');
    }

    protected function getPersonnesPager()
    {
        $pager = new sfDoctrinePager('Personne');
        $pager->setMaxPerPage(10);
        $pager->setPage($this->getPersonnesPage());
        $pager->setQuery($this->buildPersonnesQuery());
        $pager->init();

        return $pager;
    }

    protected function setPersonnesPage($page)
    {
        $this->getUser()->setAttribute('personnes.page', $page, 'frontend_module');
    }

    protected function getPersonnesPage()
    {
        return $this->getUser()->getAttribute('personnes.page', 1, 'frontend_module');
    }

    protected function buildPersonnesQuery()
    {
        if (null === $this->personnesForm) {
            $this->personnesForm = new PersonnesIndexFormFilter($this->getPersonnesFilters());
        }
        $query = $this->personnesForm->buildQuery($this->getPersonnesFilters());
        return $query;
    }

    protected function getPersonnesFilters()
    {
        return $this->getUser()->getAttribute('personnes.filters', array(), 'frontend_module');
    }

    protected function setPersonnesFilters(array $filters)
    {
        return $this->getUser()->setAttribute('personnes.filters', $filters, 'frontend_module');
    }

}
